local MapExcel = require("excel.Map")
local CopyConfig = MapExcel.copy
local Lang = require("common.Lang")
local CopyDefine = require("copy.Define")
local ActDefine = require("activity.Define")
local JZWarBaseConfig = require("excel.JzWar").Base[1]
local CCJJWarBaseConfig = require("excel.CCJJWar").Base[1]
local Copy
local DailyActFL
local DailyActFW
local DHFight
if _G.is_world then
    Copy = require("copy.CopyW")
    DailyActFW = require("activity.DailyActFW")
else
    Copy = require("copy.CopyL")
    DailyActFL = require("activity.DailyActFL")
    DHFight = require("copy.DHFight")
end
function getCopyEnterCount(human, copyID)
  local config = CopyConfig[copyID]
  for i = 0, human.db.copyTimesLen-1 do
     if human.db.copyTimes[i].copy_id == config.group then
         return human.db.copyTimes[i].times
     end
  end
  return 0
end

function getCopyMaxEnterCount(human, copyID)
  local config = CopyConfig[copyID]
  return config.enterCount + getCopyVipAddCnt(human, copyID)
end

function getCopyVipAddCnt(human, copyID)
  local config = CopyConfig[copyID]
  for i = 0, human.db.copyTimesLen-1 do
     if human.db.copyTimes[i].copy_id == config.group then
         return human.db.copyTimes[i].addCnt
     end
  end
  return 0
end

function isSameGroupCopy(copyID1, copyID2)
  local config1 = CopyConfig[copyID1]
  local config2 = CopyConfig[copyID2]
  if config1 == config2 or config1.group == config2.group then
      return true
  end
end
local DifficultDesc = {[0]="普通","挑战","困难","英雄","无双","炼狱"}
function getCopyDifficult(diff)
  return DifficultDesc[diff]
end

-- 检查是否满足副本条件
if _G.is_world then
function checkEnterCondition(human, copyID)
    local copyConfig = CopyConfig[copyID]
    if not copyConfig then
        return ""
    end
    if Copy.isCopy(human.db.map_id) then
        return Lang.COPY_ENTER_ERR_IN_COPY
    end
    if human.db.hp < 1 then
        return Lang.COPY_ENTER_ERR_DIE
    end
    -- 战斗状态判断
    if copyConfig.noExitWhenFight == 1 and human.db.fight == 1 then
        return Lang.COPY_ENTER_ERR_FIGHTING
    end
    for _,condition in ipairs(copyConfig.enterCondition) do    
        if condition[1] == "level" then
            if human.db.lv < condition[2] then
                  return string.format(Lang.COPY_ENTER_ERR_CONDITION_LV, condition[2])
            end
        elseif condition[1] == "zhandouli" then
            if human.db.zhandouli < condition[2] then
                  return string.format(Lang.COPY_ENTER_ERR_CONDITION_ZHANDOULI, condition[2])
            end
        end
    end
    if copyConfig.enterCount > 0 then
        -- 判断进入次数
        local enterTimes = getCopyEnterCount(human, copyID) 
        if enterTimes >= copyConfig.enterCount + getCopyVipAddCnt(human, copyID) then
          return Lang.COPY_ENTER_ERR_TIMES
        end
    end

    if copyID == CopyDefine.COPY_GMTX_COPY_ID then
        if not DailyActFW.onCheckActIsRunning(ActDefine.ACT_TYPE_GMTX_ACT) then
            return Lang.GMTX_NOT_START
        end
    elseif copyID == CopyDefine.COPY_JZWAR_ID then
        if not DailyActFW.onCheckActIsRunning(ActDefine.ACT_TYPE_JZWAR_ACT) then
            return Lang.JZWAR_NOT_START
        end
        local nStartTime = DailyActFW.onGetCurRunningActStartTime(ActDefine.ACT_TYPE_JZWAR_ACT)
        local nEndTime = DailyActFW.onGetCurRunningActEndTime(ActDefine.ACT_TYPE_JZWAR_ACT)
        if human.db.jzwarEnterTime >= nStartTime and human.db.jzwarEnterTime <= nEndTime then
            return Lang.JZWAR_ALD_ENTER  --只能进一次            
        end
        local nStartSplit =  os.time() - nStartTime
        if nStartSplit >= JZWarBaseConfig.beginfight then
            return Lang.JZWAR_STOP_ENTER  --停止进入
        end
    elseif copyID == CopyDefine.COPY_CCJJWAR_ID then
        if not DailyActFW.onCheckActIsRunning(ActDefine.ACT_TYPE_CCJJ) then
            return Lang.CCJJWAR_NOT_START
        end
        local nStartTime = DailyActFW.onGetCurRunningActStartTime(ActDefine.ACT_TYPE_CCJJ)
        local nEndTime = DailyActFW.onGetCurRunningActEndTime(ActDefine.ACT_TYPE_CCJJ)
        if human.db.ccjjwarEnterTime >= nStartTime and human.db.ccjjwarEnterTime <= nEndTime then
            return Lang.CCJJWAR_ALD_ENTER  --只能进一次            
        end
        local nStartSplit =  os.time() - nStartTime
        if nStartSplit >= CCJJWarBaseConfig.stopEnterTime then
            return Lang.CCJJWAR_STOP_ENTER  --停止进入
        end
    end
end

else
local Conditions = require("copy.Conditions")
local ObjHuman = require("core.ObjHuman")
function checkEnterCondition(human, copyID, noCheckCopy)
  local copyConfig = CopyConfig[copyID]
  if not copyConfig then
    assert(nil, " enterCopy no config copyID:" .. copyID)
    return ""
  end
  
  -- 当前如果在副本中 不能进入别的副本
  local nowCopyID = Copy.isCopyBySceneID(human.scene_id)
  if nowCopyID and noCheckCopy == nil then    
    return Lang.COPY_ENTER_ERR_IN_COPY
  end
  
  -- 血量判断
  if human.db.hp < 1 then
    return Lang.COPY_ENTER_ERR_DIE
  end
  
  -- 军团判断
  if copyConfig.type == CopyDefine.COPY_TYPE_FAMILY and human.db.familyUuid == nil then
    return Lang.COPY_ENTER_ERR_NO_FAMILY
  end
  
  -- 战斗状态判断
  if copyConfig.noExitWhenFight == 1 and ObjHuman.checkIsInFighting(human) then
    return Lang.COPY_ENTER_ERR_FIGHTING
  end
  
  -- 进入条件判断
  local retCondition = Conditions.handler(human, copyConfig.enterCondition) 
  if retCondition then
    return retCondition
  end 
  if copyConfig.enterCount > 0 then
    -- 判断进入次数
    local enterTimes = getCopyEnterCount(human, copyID)  
    if enterTimes >= copyConfig.enterCount + getCopyVipAddCnt(human, copyID) then
      return Lang.COPY_ENTER_ERR_TIMES  
    end
  end 
end

end

function specialCtl(human, copyID)
    local nMapID = nil
    if copyID == CopyDefine.COPY_DHFIGHT_ID1 
        or copyID == CopyDefine.COPY_DHFIGHT_ID2 
        or copyID == CopyDefine.COPY_DHFIGHT_ID3 
        or copyID == CopyDefine.COPY_DHFIGHT_ID4 
        or copyID == CopyDefine.COPY_DHFIGHT_ID5 then
        local nActData = DailyActFL.onGetActData(ActDefine.ACT_TYPE_DHFIGHT_ACT)
        if not nActData or nActData.status ~= ActDefine.ACT_STATE_RUNNING then
            return copyID
        else
            local nCurFloor = DHFight.DHFIGHT_FLOOR_1 
            if human.db.dhfightEnterTime >= nActData.startTime and human.db.dhfightEnterTime <= nActData.endTime then
                nCurFloor = human.db.dhfightcurfloor
            end

            if nCurFloor and nCurFloor > 0 then
                if nCurFloor >= DHFight.DHFIGHT_FLOOR_2 then
                    copyID = CopyDefine.COPY_DHFIGHT_ID3
                end

                if nCurFloor >= DHFight.DHFIGHT_FLOOR_3 then
                    copyID = CopyDefine.COPY_DHFIGHT_ID2
                end

                if nCurFloor >= DHFight.DHFIGHT_FLOOR_4 then
                    copyID = CopyDefine.COPY_DHFIGHT_ID4
                end

                if nCurFloor >= DHFight.DHFIGHT_FLOOR_5 then
                    copyID = CopyDefine.COPY_DHFIGHT_ID5
                end

                --local nTemp = nCurFloor
                --if copyID == CopyDefine.COPY_DHFIGHT_ID2 then
                --    nTemp = nCurFloor % 3 + 1
                --elseif copyID == CopyDefine.COPY_DHFIGHT_ID3 then
                --    nTemp = nCurFloor - 1
                --end
                nMapID = CopyConfig[copyID].mapList[1]
            end
        end
    --elseif then
    end
    return copyID, nMapID
end

